--- title: "AB路线" created: 2025-11-28 tags: - 算法 --- # AB路线 ## 题目 [AB路线](https://www.lanqiao.cn/problems/17109/learning/) ![[image-15fac6c3.png]] ## 思路分析 ## 代码实现 ```cpp #include using namespace std; #define endl '\n' //bfs 多了一个限制条件 计数器 计数器未达k时 得一直走相同的格子 计数器达到k时 只能走不同的格子 typedef pair PII; typedef pair PPI; const int N=1010; char g[N][N]; int n,m,k; int dist[N][N]; int dx[]={-1,0,1,0}; int dy[]={0,1,0,-1}; bool isVaild(int x,int y){ return x>=1 && x<=n && y>=1 && y<=m && dist[x][y]==-1; } void bfs(int x,int y,int step){ memset(dist,-1,sizeof dist); queue q; q.push({{x,y},step}); dist[x][y]=0; while(!q.empty()){ auto cur=q.front();q.pop(); int ux=cur.first.first,uy=cur.first.second,ut=cur.second; if(ux==n && uy==m){ cout<>n>>m>>k; string tmp; getline(cin,tmp); for(int i=1;i<=n;i++){ getline(cin,tmp); for(int k=0,j=1;k